"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const index = require("../popup/index.cjs");
require("../icons/index.cjs");
const antDesignVue = require("ant-design-vue");
const index$1 = require("../empty/index.cjs");
const plusIcon = require("../icons/plus-icon.vue.cjs");
const editIcon = require("../icons/edit-icon.vue.cjs");
const deleteIcon = require("../icons/delete-icon.vue.cjs");
const _hoisted_1 = { class: "me-common-phrase-popup" };
const _hoisted_2 = { class: "me-common-phrase-popup-title" };
const _hoisted_3 = { class: "me-common-phrase-content" };
const _hoisted_4 = ["onClick"];
const _hoisted_5 = { class: "me-common-phrase-index" };
const _hoisted_6 = { class: "me-common-phrase-text" };
const _hoisted_7 = { class: "me-common-phrase-setting-popup" };
const _hoisted_8 = { class: "me-common-phrase-setting-popup-title" };
const _hoisted_9 = { class: "me-common-phrase-setting-popup-index" };
const _hoisted_10 = {
  key: 0,
  class: "me-common-phrase-setting-popup-text"
};
const _hoisted_11 = { class: "me-common-phrase-setting-popup-bottom" };
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    name: "me-common-phrase"
  },
  __name: "common-phrase",
  props: {
    trigger: { default: "click" },
    dataSource: { default() {
      return [];
    } },
    popup: { default() {
      return {
        width: "400px",
        height: "300px",
        draggable: true,
        mask: true,
        title: "常用语"
      };
    } },
    settingPopup: { default() {
      return {
        width: "400px",
        height: "300px",
        draggable: true,
        mask: true,
        title: "常用语配置"
      };
    } }
  },
  emits: ["select", "edit", "add", "delete"],
  setup(__props, { emit: __emit }) {
    const emits = __emit;
    const props = __props;
    const disabled = vue.ref(false);
    const showPopup = vue.ref(false);
    const showSettingPopup = vue.ref(false);
    const wordList = vue.ref([]);
    const editWordList = vue.ref([]);
    const input = vue.ref();
    const settingContainer = vue.ref();
    const operationState = vue.ref("");
    const currentWord = vue.ref();
    vue.watchEffect(() => {
      const list = props.dataSource.map((item) => {
        item.isEdit = false;
        return item;
      });
      wordList.value = JSON.parse(JSON.stringify(list));
    });
    const onClose = () => {
      showPopup.value = false;
    };
    const onHandle = (evt) => {
      evt.preventDefault();
      showPopup.value = true;
    };
    const onSetting = () => {
      showSettingPopup.value = true;
      editWordList.value = JSON.parse(JSON.stringify(wordList.value));
    };
    const checkIsExist = (text) => {
      return editWordList.value.some((item) => item.text === text && !item.isEdit);
    };
    const onEnter = (item) => {
      var _a;
      const text = (_a = currentWord.value) == null ? void 0 : _a.text.trim();
      if (checkIsExist(text)) {
        antDesignVue.message.warning(`已经存在：${text}，不用重复添加！`);
        return;
      }
      if (!(text == null ? void 0 : text.trim())) {
        antDesignVue.message.warning("不能为空！");
        return;
      }
      disabled.value = false;
      item.isEdit = false;
      if (operationState.value === "add" && text) {
        emits("add", currentWord.value);
      } else if (operationState.value === "edit" && text) {
        emits("edit", currentWord.value);
      }
    };
    const onEdit = (item) => {
      editWordList.value.map((item2) => {
        item2.isEdit = false;
      });
      item.isEdit = true;
      disabled.value = true;
      vue.nextTick(() => {
        input.value[0].focus();
      });
      operationState.value = "edit";
      currentWord.value = item;
    };
    const onDelete = (item) => {
      if (item.isEdit) {
        disabled.value = false;
      }
      editWordList.value = editWordList.value.filter((word) => word.text !== item.text);
      emits("delete", item);
    };
    const addWord = () => {
      if (disabled.value) {
        return;
      }
      disabled.value = true;
      const word = {
        text: "",
        isEdit: true
      };
      editWordList.value.push(word);
      vue.nextTick(() => {
        settingContainer.value.scrollTop = settingContainer.value.scrollHeight;
        input.value[0].focus();
      });
      operationState.value = "add";
      currentWord.value = word;
    };
    const clickWord = (item) => {
      emits("select", item);
      onClose();
    };
    const checkIsEdit = () => {
      return editWordList.value.some((item) => item.isEdit);
    };
    const onSettingClose = () => {
      disabled.value = false;
      wordList.value = wordList.value.filter((item) => item.text && !item.isEdit);
    };
    const onCancel = () => {
      if (checkIsEdit()) {
        antDesignVue.message.warning("正在编辑中！");
        return;
      }
      showSettingPopup.value = false;
    };
    const onConfirm = () => {
      if (checkIsEdit()) {
        antDesignVue.message.warning("正在编辑中！");
        return;
      }
      showSettingPopup.value = false;
      wordList.value = JSON.parse(JSON.stringify(editWordList.value));
    };
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("div", vue.mergeProps({ [vue.toHandlerKey(_ctx.trigger)]: onHandle }, { class: "me-common-phrase" }), [
        vue.renderSlot(_ctx.$slots, "default"),
        vue.createVNode(vue.unref(index.Popup), vue.mergeProps({
          value: showPopup.value,
          "onUpdate:value": _cache[0] || (_cache[0] = ($event) => showPopup.value = $event)
        }, _ctx.popup, { onClose }), {
          default: vue.withCtx(() => [
            vue.createElementVNode("div", _hoisted_1, [
              vue.createElementVNode("div", _hoisted_2, [
                vue.createVNode(vue.unref(antDesignVue.Button), {
                  class: "me-common-phrase-btn",
                  onClick: onSetting,
                  type: "primary"
                }, {
                  default: vue.withCtx(() => [
                    vue.createVNode(vue.unref(plusIcon.default), { class: "me-common-phrase-plus-icon" }),
                    _cache[2] || (_cache[2] = vue.createTextVNode("配置 "))
                  ]),
                  _: 1
                })
              ]),
              vue.createElementVNode("div", _hoisted_3, [
                wordList.value.length > 0 ? (vue.openBlock(true), vue.createElementBlock(vue.Fragment, { key: 0 }, vue.renderList(wordList.value, (item, index2) => {
                  return vue.openBlock(), vue.createElementBlock("div", {
                    onClick: ($event) => clickWord(item),
                    class: "me-common-phrase-content-item",
                    key: index2
                  }, [
                    vue.createElementVNode("span", _hoisted_5, vue.toDisplayString(index2 + 1) + "、", 1),
                    vue.createElementVNode("span", _hoisted_6, vue.toDisplayString(item.text), 1)
                  ], 8, _hoisted_4);
                }), 128)) : (vue.openBlock(), vue.createBlock(vue.unref(index$1.Empty), {
                  key: 1,
                  description: "暂无数据"
                }))
              ])
            ])
          ]),
          _: 1
        }, 16, ["value"]),
        vue.createVNode(vue.unref(index.Popup), vue.mergeProps({
          value: showSettingPopup.value,
          "onUpdate:value": _cache[1] || (_cache[1] = ($event) => showSettingPopup.value = $event)
        }, _ctx.settingPopup, { onClose: onSettingClose }), {
          default: vue.withCtx(() => [
            vue.createElementVNode("div", _hoisted_7, [
              vue.createElementVNode("div", _hoisted_8, [
                vue.createVNode(vue.unref(antDesignVue.Button), {
                  disabled: disabled.value,
                  class: "me-common-phrase-btn",
                  onClick: addWord,
                  type: "primary"
                }, {
                  default: vue.withCtx(() => [
                    vue.createVNode(vue.unref(plusIcon.default), { class: "me-common-phrase-plus-icon" }),
                    _cache[3] || (_cache[3] = vue.createTextVNode("添加 "))
                  ]),
                  _: 1
                }, 8, ["disabled"])
              ]),
              vue.createElementVNode("div", {
                ref_key: "settingContainer",
                ref: settingContainer,
                class: "me-common-phrase-setting-popup-content"
              }, [
                editWordList.value.length > 0 ? (vue.openBlock(true), vue.createElementBlock(vue.Fragment, { key: 0 }, vue.renderList(editWordList.value, (item, index2) => {
                  return vue.openBlock(), vue.createElementBlock("div", {
                    class: vue.normalizeClass([{ edit: item.isEdit }, "me-common-phrase-setting-popup-item"]),
                    key: index2
                  }, [
                    vue.createElementVNode("span", _hoisted_9, vue.toDisplayString(index2 + 1) + "、", 1),
                    !item.isEdit ? (vue.openBlock(), vue.createElementBlock("span", _hoisted_10, vue.toDisplayString(item.text), 1)) : (vue.openBlock(), vue.createBlock(vue.unref(antDesignVue.Input), {
                      key: 1,
                      onPressEnter: ($event) => onEnter(item),
                      ref_for: true,
                      ref_key: "input",
                      ref: input,
                      class: "me-common-phrase-setting-popup-text",
                      value: item.text,
                      "onUpdate:value": ($event) => item.text = $event
                    }, null, 8, ["onPressEnter", "value", "onUpdate:value"])),
                    vue.createVNode(vue.unref(editIcon.default), {
                      onClick: ($event) => onEdit(item),
                      class: "me-common-phrase-setting-popup-edit-icon"
                    }, null, 8, ["onClick"]),
                    vue.createVNode(vue.unref(deleteIcon.default), {
                      onClick: ($event) => onDelete(item),
                      class: "me-common-phrase-setting-popup-delete-icon"
                    }, null, 8, ["onClick"])
                  ], 2);
                }), 128)) : (vue.openBlock(), vue.createBlock(vue.unref(index$1.Empty), {
                  key: 1,
                  description: "暂无数据"
                }))
              ], 512),
              vue.createElementVNode("div", _hoisted_11, [
                vue.createVNode(vue.unref(antDesignVue.Button), {
                  onClick: onCancel,
                  class: "me-common-phrase-setting-popup-bottom-btn"
                }, {
                  default: vue.withCtx(() => _cache[4] || (_cache[4] = [
                    vue.createTextVNode("取消")
                  ])),
                  _: 1
                }),
                vue.createVNode(vue.unref(antDesignVue.Button), {
                  type: "primary",
                  onClick: onConfirm,
                  class: "me-common-phrase-setting-popup-bottom-btn"
                }, {
                  default: vue.withCtx(() => _cache[5] || (_cache[5] = [
                    vue.createTextVNode("确定")
                  ])),
                  _: 1
                })
              ])
            ])
          ]),
          _: 1
        }, 16, ["value"])
      ], 16);
    };
  }
});
exports.default = _sfc_main;
//# sourceMappingURL=common-phrase.vue2.cjs.map
